
#include "STC32G_Timer.h"
#include "STC32G_GPIO.h"
#include "STC32G_NVIC.h"
#include "STC32G_Exti.h"
#include "STC32G_Delay.h"

#include "app_config.h"
#include "drv_jiffes.h"
#include "drv_key.h"
#include "drv_key_io.h"
#include "drv_key_ad.h"
#include "debug.h"


#if TCFG_DRV_KEY_SUPPORT

static jiffes_t key_jiffes;

static key_code_t key_code;

static struct key_interface_t *key_list[] = {
#if TCFG_DRV_IOKEY_SUPPORT
    &key_io_info,
#endif
#if TCFG_DRV_ADKEY_SUPPORT
    &key_ad_info,
#endif
};

void drv_key_init(void)
{
    int i;
    for (i=0; i<(sizeof(key_list)/sizeof(key_list[0])); i++) {
        if (key_list[i]->key_init) {
            key_list[i]->key_init();
        }
    }
}

key_value_t get_key_value(void)
{
    int i;
    key_value_t key;
    key.key_type = NO_KEY;

    for (i=0; i<(sizeof(key_list)/sizeof(key_list[0])); i++) {
        if (key_list[i]->key_get_value) {
            key.key_num = key_list[i]->key_get_value();
        }

        if (NO_KEY != key.key_num) {
            key.key_type = key_list[i]->key_type;
            return key;
        }
    }

    key.key_num = NO_KEY;
    return key;
}

key_code_t drv_key_value_get(void)
{
    key_code_t temp = key_code;
    key_code.key_type = KEYCODE_TYPE_NONE;
    return temp;
}

void drv_key_value_set(uint8_t keycode_type, uint8_t key_type, uint8_t key_num)
{
    uint8_t keycode = 0;

    if (KEY_TYPE_IO == key_type) {
        keycode = key_num;
    } else if (KEY_TYPE_AD == key_type) {
        keycode = key_num;
    }

    key_code.key_type = keycode_type;
    key_code.key_code = keycode;
}

// 10MS 周期
void drv_key_scan(void)
{
    static uint8_t key_type = NO_KEY;
    static uint8_t last_key = NO_KEY;   // 记录上次的按键值
    static uint8_t key_press_flag = 0;
    static uint8_t key_double_flag = 0;
    static uint8_t key_press_counter = 0;
    static uint8_t key_double_counter = 0;

    key_value_t key_io, key_ad;
    uint8_t cur_key=NO_KEY;

    key_value_t key = get_key_value();

    if (key.key_num != NO_KEY) {
        // TODO:按键有效
        key_type = key.key_type;
        cur_key = key.key_num;
        key_press_counter++;

        if ((key_press_counter > 3) && (key_press_flag == 0)) {
            // TODO: 短按按下
            key_press_flag = 1;
            key_double_flag++;

            // log_d("key_double_flag:%d\n", key_double_flag);
            if (key_double_counter && (last_key == cur_key)) {  
                if (key_double_flag == 2) {
                    key_press_counter = 0;
                    drv_key_value_set(KEYCODE_TYPE_DCLICK, key_type, last_key);
                    // if (KEY_TYPE_IO == key_type) {
                        // log_d("IOKEY DOUBLE CLICK key_num:%d\n", last_key);
                    // }
                }          
                
            } else {
                last_key = cur_key;                                              
            }
            key_double_counter = 35;
            
        } else if ((key_press_counter > 80) && (key_press_flag == 1)) { 
            key_press_flag = 2;
            key_press_counter = 0;
            drv_key_value_set(KEYCODE_TYPE_LONG_DOWN, key_type, last_key);
            // if (KEY_TYPE_IO == key_type) {
                // log_d("IOKEY LONG DOWN key_num:%d\n", last_key);
            // }
        }  else if ((key_press_counter > 30) && (key_press_flag == 2)) { 
            key_press_counter = 0;
            key_double_flag = 0;
            drv_key_value_set(KEYCODE_TYPE_LONG_HOLD, key_type, last_key);
            // if (KEY_TYPE_IO == key_type) {
                // log_d("IOKEY HOLD  key_num:%d\n", last_key);
            // }
        }

    } else {
        // TODO:按键释放
        if ((key_double_counter==0)) {                
            if (key_double_flag == 1) {
                drv_key_value_set(KEYCODE_TYPE_CLICK, key_type, last_key);
                // if (KEY_TYPE_IO == key_type) {
                    // log_d("IOKEY SHORT CLICK key_num:%d\n", last_key);
                // }
            }
            key_double_flag = 0;           
        }

        if (key_press_flag == 2) {            
            drv_key_value_set(KEYCODE_TYPE_LONG_UP, key_type, last_key);
            // if (KEY_TYPE_IO == key_type) {
                // log_d("IOKEY LONG UP key_num:%d\n", last_key);
            // }
            key_double_flag = 0;    // 防止LONG UP之后触发SHORT CLICK
        }
        
        key_press_flag = 0;
        key_press_counter = 0;        
    }

    // TODO：
    if (key_double_counter) {
        key_double_counter--;
    }
}


#endif // TCFG_DRV_KEY_SUPPORT

